package com.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



import com.entity.Detail;
import com.entity.Plan;
import com.service.DetailService;
import com.service.PlanService;



@Controller
public class PlanController {
@Resource
private PlanService planService;
@Resource
private DetailService detailService;	
private int count;
@RequestMapping(value="/index")	
public String index(int index,Model model){
	int re=planService.getCountPlan();
	count=re/3;
  int count2=re%3;
	if(count2>0){
		count=count+1;
	}
	model.addAttribute("count",count);
	if(index<=0){
		index=1;
	}else if(index>=count){
		index=count;
	}
	
	int a=0;
	if(index>=1){
	a=(index-1)*3;
	}
	
	List<Plan>list=planService.getlist(a);
	model.addAttribute("list",list);
	model.addAttribute("index",index);
	
	return "index";
}	
@RequestMapping(value="/add")	
public String getmaxid(Model model){
 String	maxpid=getNewId();
	model.addAttribute("maxpid",maxpid);
	return "add";
}
@RequestMapping("/planadd")	
public String planadd(Plan plan){
	System.out.println(plan.getPname());
    planService.insert(plan);
	return "redirect:/index?index=1";
}
@RequestMapping("/plangetById")	
public String plangetById(String pid,Model model){
  Plan plan=planService.selectByPrimaryKey(pid);
  List<Detail>list=detailService.getById(pid);

  planService.updateByPrimaryKey(plan);
  model.addAttribute("plan",plan);
  model.addAttribute("dlist",list);
	return "update";
}
@RequestMapping("/planupdate")	
public String planupdate(Plan plan){
 planService.updateByPrimaryKey(plan);
return "redirect:/index?index=1";
}
@RequestMapping("/plandelete")	
public String plandelete(String pid){
List<Detail>list=detailService.getById(pid);
if(list.size()>0){
	return "error";
}else{
 planService.deleteByPrimaryKey(pid);

 
return "redirect:/index?index=1";
}
}

public String getNewId() {
	String planId =planService.getMaxid();
	Pattern p = Pattern.compile("[0-9]{4,}");
	Matcher m = p.matcher(planId);
	String[] id = new String[2];
	int i = 0;
	while (m.find()) {
		id[i] = m.group();
		i++;
	}
	Date date = new Date();
	// 转换提日期输出格式
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
	if (id[0].equals(dateFormat.format(date))) {
		String a = Integer.toString((Integer.parseInt(id[1]) + 1));
		if (a.length() == 1)
			id[1] = "0000" + a;
		else if (a.length() == 2)
			id[1] = "000" + a;
		else if (a.length() == 3)
			id[1] = "00" + a;
		else if (a.length() == 4)
			id[1] = "0" + a;
		else if (a.length() == 5)
			id[1] = a;
	} else {
		String a = Integer.toString((Integer.parseInt(dateFormat.format(date))));
		id[0] = a;
		id[1] = "00000";
	}
	String newId = "pl" + id[0] + "-" + id[1];
	return newId;
}
}
